java根据word模板生成word文件

您所在的位置:网站首页 word 模板文件扩展名 java根据word模板生成word文件

java根据word模板生成word文件

2023-09-22 20:57| 来源: 网络整理| 查看: 265

本文使用的feemarker实现的功能 1、首先新建一个word模板, 2、另存为xml格式, 3、将xml文件的扩展名直接改成ftl, 4、导入freemaker.jar 在这里插入图片描述 2、使用软件打开ftl文件,找到w:binData快速定位图片位置,写上${imgStr}(名字随意,和java代码中的对应上即可),然后保存。 3、因涉及到list的问题,找到需要设计list的行,找到w:tr,将w:tr包裹。 在这里插入图片描述 在这里插入图片描述 3、新建java类

@Component public class ExamTemplate { public String saveExamTable(Welder welder,ExamRegis examRegis,List wExperiences,List weldQuas, List trains,List examProjects,String templateUrl,Inframework inframework,HttpServletRequest req)throws Exception { Map dataMap=new HashMap(); dataMap.put("depName", inframework.getInframeworkName()); dataMap.put("code", inframework.getCode()); dataMap.put("address", inframework.getAddress()); dataMap.put("linker", inframework.getLinker()); String regisNum=examRegis.getRegisNum(); dataMap.put("regisNum", regisNum); //dataMap.put("startTime", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse("2018-09-19"))); dataMap.put("idNumber", welder.getIdNumber()); dataMap.put("name",welder.getName()); int sexInt=welder.getSex(); if(sexInt==1) { dataMap.put("sex", "男"); }else { dataMap.put("sex", "女"); } //dataMap.put("fukuan", "test"); dataMap.put("imgStr",getImageStr(welder,templateUrl)); dataMap.put("theoryNum",examRegis.getTheoryNum()); //dataMap.put("workExperience", "叁仟肆佰万"); //List weldQuas=new ArrayList(); dataMap.put("wExperiences", wExperiences); dataMap.put("weldQuas", weldQuas); dataMap.put("trains", trains); dataMap.put("examProjects", examProjects); Configuration configuration=new Configuration(); configuration.setDefaultEncoding("utf-8"); configuration.setDirectoryForTemplateLoading(new File(templateUrl)); Calendar calendar=Calendar.getInstance(); int monthInt=calendar.get(Calendar.MONTH)+1; int year=calendar.get(Calendar.YEAR); String newName=UUID.randomUUID().toString(); File outFile=new File(templateUrl+year+"/"+monthInt+"/"+newName+".doc"); Template template=configuration.getTemplate("报名表.ftl","utf-8"); Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"),1024); template.process(dataMap,out); out.flush(); out.close(); String url=req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+"/WeldCloud/upload/"+year+"/"+monthInt+"/"+newName+".doc"; return url; } public String getImageStr(Welder welder,String templateUrl) throws Exception{ String[] path=welder.getPhotoUrl().split("/"); String imgFile=""; if(path.length==8) { imgFile=templateUrl+path[5]+"/"+path[6]+"/"+path[7]; } InputStream in=null; byte[] data=null; in=new FileInputStream(imgFile); data=new byte[in.available()]; in.read(data); in.close(); BASE64Encoder encoder=new BASE64Encoder(); return encoder.encode(data); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3